﻿using UnityEngine;
using USNetFramework.Network;
using Sproto;

namespace NetFramework
{
    public enum SocketEvent
    {
        OnConnected,
        OnConnectTimout,
        OnDisconnected,
    }

    public delegate void OnSocketEvent(SocketEvent _event, NetSocket _socket);

    public class NetworkHandler : ISocketListener
    {
        private event OnSocketEvent SocketEventHandler;

        public void RegisterSocketEvent(OnSocketEvent _handler)
        {
            SocketEventHandler += _handler;
        }

        public void UnregisterSocketEvent(OnSocketEvent _handler)
        {
            SocketEventHandler -= _handler;
        }

        // Socket处理消息时
        public virtual bool OnHandleMessage(NetSocket _socket, NetPacket _packet)
        {
            return true;
        }

        // Socket连接时调用
        public virtual void OnSocketConnected(NetSocket _socket)
        {
            Debug.Log("OnSocketConnected");
            SocketEventHandler?.Invoke(SocketEvent.OnConnected, _socket);
        }

        // Socket超时时调用
        public virtual void OnSocketConnectTimout(NetSocket _socket)
        {
            Debug.Log("OnSocketDisconnected");
            SocketEventHandler?.Invoke(SocketEvent.OnConnectTimout, _socket);
        }

        // Socket断开时调用
        public virtual void OnSocketDisconnected(NetSocket _socket)
        {
            Debug.Log("OnSocketDisconnected");
            SocketEventHandler?.Invoke(SocketEvent.OnDisconnected, _socket);
        }
    }
}
